package com.data.interceptor;

import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.data.common.api.dto.OnlineAuthDTO;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.OnlineAuth;
import com.data.common.system.api.ISysBaseAPI;
import com.data.common.system.util.JwtUtil;
import com.data.common.util.SpringContextUtils;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.service.IOnlineBaseAPI;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

@SuppressWarnings("ALL")
@Slf4j
public class OnlineInterceptor implements HandlerInterceptor {

    private static final Logger f53a = LoggerFactory.getLogger(OnlineInterceptor.class);

    private static final String f54d = "/online/cgform";

    private static final String[] f55e = {"/online/cgformInnerTableList", "/online/cgformErpList", "/online/cgformList", "/online/cgformTreeList", "/online/cgformTabList"};

    private IOnlineBaseAPI f56b;

    private ISysBaseAPI f57c;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        OnlineAuth methodAnnotation;
        String str;
        if (handler.getClass().isAssignableFrom(HandlerMethod.class) && (methodAnnotation = ((HandlerMethod) handler).getMethodAnnotation(OnlineAuth.class)) != null) {
            String a = replaceSlash(request.getRequestURI().substring(request.getContextPath().length()));
            String value = methodAnnotation.value();
            String substring = a.substring(a.lastIndexOf(value) + value.length());
            if (!CgformUtil.f276al.equals(value) || !"DELETE".equals(request.getMethod())) {
                str = substring;
            } else {
                str = substring.substring(0, substring.lastIndexOf("/"));
            }
            String parameter = request.getParameter("tabletype");
            if (this.f56b == null) {
                this.f56b = (IOnlineBaseAPI) SpringContextUtils.getBean(IOnlineBaseAPI.class);
            }
            String onlineErpCode = this.f56b.getOnlineErpCode(str, parameter);
            ArrayList arrayList = new ArrayList();
            String[] strArr = f55e;
            int length = strArr.length;
            for (int i = 0; i < length; i++) {
                arrayList.add(strArr[i] + onlineErpCode);
            }
            if (this.f57c == null) {
                this.f57c = (ISysBaseAPI) SpringContextUtils.getBean(ISysBaseAPI.class);
            }
            if (!this.f57c.hasOnlineAuth(new OnlineAuthDTO(JwtUtil.getUserNameByToken(request), arrayList, f54d))) {
                writer(response, value);
                return false;
            }
        }
        return true;
    }

    private String replaceSlash(String str) {
        if (!ConvertUtils.isNotEmpty(str)) {
            return "";
        }
        String replace = str.replace("\\", "/").replace("//", "/");
        if (replace.indexOf("//") >= 0) {
            return replaceSlash(replace);
        }
        return replace;
    }

    private void writer(HttpServletResponse httpServletResponse, String str) {
        PrintWriter printWriter = null;
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json; charset=utf-8");
        httpServletResponse.setHeader("auth", "fail");
        try {
            PrintWriter writer = httpServletResponse.getWriter();
            if ("exportXls".equals(str)) {
                writer.print("");
            } else {
                writer.print(JSON.toJSON(Result.error("无权限访问(操作)")));
            }
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            f53a.error(e.getMessage());
            if (0 != 0) {
                printWriter.close();
            }
        } catch (Throwable th) {
            if (0 != 0) {
                printWriter.close();
            }
            throw th;
        }
    }
}
